// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BaseView from "../base/BaseView";
import StockListComponent from "../base/components/StockListComponent";
import ThsInfoListComponent from "../base/components/ThsInfoListComponent";
import FileModule from "../module/FileModule";
import LogModule from "../module/LogModule";
import MatchModule from "../module/MatchModule";
import SeptkapaModule from "../module/SeptkapaModule";
import TimeModule from "../module/TimeModule";
import TushareModule from "../module/TushareModule";
import DropList from "./items/DropList";
import { ThsRankData } from "./models/ThsRankData";

const {ccclass, property} = cc._decorator;

const PlateDaysRankConfigKey = 'plate_days_rank_config';

var DropConfig = [
    {
        key: 'plateType',
        data: ['全部', '行业', '细分行业', '概念'],
        value: [0, 1, 2, 3],
        defaultIndex: 0,
    },
    {
        key: 'tradeDayCnt',
        data: ['1', '2', '3', '5', '8', '10', '15', '20', '30', '50', '80', '100', '120', '240'],
        value: [1, 2, 3, 5, 8, 10, 15, 20, 30, 50, 80, 100, 120, 240],
        defaultIndex: 0,
    },
    {   //
        key: 'upRank',
        data: ['升序', '降序'],
        value: [true, false],
        defaultIndex: 0,
    },
];

@ccclass
export default class ThsDaysRankView extends BaseView {

    // LIFE-CYCLE CALLBACKS:
    @property([DropList])
    dropLists: DropList[] = [];

    @property(cc.Prefab)
    plateListPrefab: cc.Prefab = null;

    @property(cc.Node)
    plateListLayer: cc.Node = null;

    plateListComp: ThsInfoListComponent = null;

    _searchPlates: any = [];

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        let lastSearchConfig = SeptkapaModule.getInstance().getValue(PlateDaysRankConfigKey);

        for (let index = 0; index < this.dropLists.length; index++) {
            const drop = this.dropLists[index];
            let config = DropConfig[index];
            let selectIndex = config.defaultIndex;
            if (lastSearchConfig && lastSearchConfig[config.key] !== undefined) {
                selectIndex = lastSearchConfig[config.key];
            }
            drop.setData(config.data, selectIndex);
        }

        let node = cc.instantiate(this.plateListPrefab);
        this.plateListLayer.addChild(node);
        this.plateListComp = node.getComponent(ThsInfoListComponent);
    }

    start () {

    }

    protected onEnable(): void {
        cc.director.on('SelectStockEvent', this.onSelectStockEvent, this);
    }

    protected onDisable(): void {
        cc.director.off('SelectStockEvent', this.onSelectStockEvent, this);
    }

    onSelectStockEvent (selectIndex) {
        // this.createDialog(BaseView.Views.StockInfoView, {stocks: this._searchStocks, selectIndex: selectIndex} );
    }

    async onBtnSearch () {
        let condition = {};
        let searchConfig = {};
        for (let index = 0; index < this.dropLists.length; index++) {
            const drop = this.dropLists[index];
            let config = DropConfig[index];
            let selectIndex = drop.getSelectIndex();
            condition[config.key] = config.value[selectIndex];
            searchConfig[config.key] = selectIndex;
        }
        SeptkapaModule.getInstance().setValue(PlateDaysRankConfigKey, searchConfig);
        let days = TimeModule.getInstance().getRecentTradeDates(condition['tradeDayCnt']);
        let module = TushareModule.getInstance();
        let startDate = parseInt(days[days.length - 1]);
        let endDate = parseInt(days[0]);
        module.loadThsData(startDate, endDate, (event: string, data: any) => {
            this.onTushareEvent(event, data);
        });
    }

    onTushareEvent (event: string, data: any) {
        if (event == 'LoadStart') {

        } else if (event == 'LoadFailed') {
            this.showToast(data.msg);
        } else if (event == 'LoadOver') {
            this.showToast('拉取数据完成');
            LogModule.log('拉取数据完成');
            this.updateView();
        } else {
            LogModule.log('拉取到:' + data.date);
        }
    }

    updateView () {
        let condition = {};
        for (let index = 0; index < this.dropLists.length; index++) {
            const drop = this.dropLists[index];
            let config = DropConfig[index];
            let selectIndex = drop.getSelectIndex();
            condition[config.key] = config.value[selectIndex];
        }
        let list = ThsRankData.getInstance().getPlateDaysData(condition['tradeDayCnt']);
        this._searchPlates = list;
    }

    // update (dt) {}
}
